import type {UseMouseEventParam} from './type';
import {throttle,bindEvent} from '../../util';

export default function({start,end,move,cursor,throttle:time}:UseMouseEventParam):(e:MouseEvent) => void{
	const [mousemoveFn,flushFn]=(function(){
		if(time===false){
			return [move,function(){}];
		}else{
			const fn=throttle(move,time);
			return [fn,fn.flush];
		}
	})();
	const [on,off]=bindEvent([
		{type:'mousemove',listener:mousemoveFn},
		{type:'mouseup',listener:mouseupFn},
	],{
		passive:true,
	});
	function mouseupFn(e:MouseEvent){
		off();
		document.body.classList.remove('pf_global--noSelectable','pf_global--noPointerEvents');
		if(cursor){
			document.documentElement.style.cursor='';
		}
		flushFn();
		end&&end(e);
	}
	return function(e:MouseEvent){
		on();
		document.body.classList.add('pf_global--noSelectable','pf_global--noPointerEvents');
		if(cursor){
			document.documentElement.style.cursor=cursor;
		}
		start(e);
	};
}
export {
	UseMouseEventParam,
};